package com.ledim.adapter;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.facebook.drawee.view.SimpleDraweeView;
import com.ledim.adapter.base.LedimBaseListAdapter;
import com.ledim.bean.LedimChoiceCardBean;
import com.ledim.utils.FrescoUtil;
import com.ledim.utils.LedimFunctionUtils;
import com.letv.android.young.client.R;

/**
 * Created by ccx on 16/11/28.
 * 卡片9 item
 */

public class Card9ListAdapter extends LedimBaseListAdapter<LedimChoiceCardBean> {

    private String mGroupId;

    public Card9ListAdapter(Context context, String cardGroupId) {
        super(context);
        mGroupId = cardGroupId;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.card_a9_item, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        final LedimChoiceCardBean cardBean = getItem(position);
        if (TextUtils.isEmpty(cardBean.title)) {
            holder.mTitle.setVisibility(View.GONE);
        } else {
            holder.mTitle.setVisibility(View.VISIBLE);
            holder.mTitle.setText(cardBean.title);
        }

        if (TextUtils.isEmpty(cardBean.subtitle)) {
            holder.mSubTitle.setVisibility(View.GONE);
        } else {
            holder.mSubTitle.setVisibility(View.VISIBLE);
            holder.mSubTitle.setText(cardBean.subtitle);
        }
        if (cardBean.photo != null && !TextUtils.isEmpty(cardBean.photo.thumb)) {
            FrescoUtil.loadUrl(cardBean.photo.thumb, holder.mImage);
        }
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LedimFunctionUtils.jumpTargetActivity((Activity) context, cardBean, mGroupId);
            }
        });
        return convertView;
    }

    static class ViewHolder {

        SimpleDraweeView mImage;
        TextView mTitle;
        TextView mSubTitle;

        public ViewHolder(View convertView) {
            mImage = (SimpleDraweeView) convertView.findViewById(R.id.card_item_photo);
            mTitle = (TextView) convertView.findViewById(R.id.card_item_title);
            mSubTitle = (TextView) convertView.findViewById(R.id.card_item_subtitle);
        }
    }
}
